﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Threading;
using System.Collections.Generic;

namespace ISolutions.SharePoint.Mui.MuiListFilterWebPart
{

    /// <summary>
    /// MUI List Filter Web Part. Filters a out of the box List View Web Part for the currrent user language.
    /// 
    /// by David Schneider, isolutions AG, http://blog.sharepoint.ch
    /// </summary>
    [ToolboxItemAttribute(false)]
    public class MuiListFilterWebPart : Microsoft.SharePoint.WebPartPages.WebPart, ITransformableFilterValues
    {
        [ConnectionProvider(
        "Language",
        "CurrentLanguage",
        AllowsMultipleConnections = true)]
        public ITransformableFilterValues SetConnection()
        {
            return this;
        }

        public bool AllowAllValue
        {
            get { return true; }
        }

        public bool AllowEmptyValue
        {
            get { return true; }
        }

        public bool AllowMultipleValues
        {
            get { return true; }
        }

        public string ParameterName
        {
            get { return "Language"; }
        }

        /// <summary>
        /// Gets the current user language
        /// </summary>
        /// <value></value>
        public System.Collections.ObjectModel.ReadOnlyCollection<string> ParameterValues
        {
            get
            {
                System.Collections.Generic.List<string> languages = new List<string>();
                languages.Add(Thread.CurrentThread.CurrentUICulture.LCID.ToString());
                return new System.Collections.ObjectModel.ReadOnlyCollection<string>(languages);
            }
        }

        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            if (base.DesignMode)
            {
                base.ChromeType = PartChromeType.TitleOnly;
                Literal lit = new Literal();
                lit.Text = "Use the Web Part Context Menu to connect this web part to a List / Document Library View Web Part.";
                this.Controls.Add(lit);
            }
            else
            {
                base.ChromeType = PartChromeType.None;
            }
        }
    }
}
